<template>
  <div class="container">
    <div class="top">
      <!-- <el-select v-model="search.type" placeholder="请选择类型">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>-->
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="search.code"
        placeholder="请输入单据编号"
        clearable
      ></el-input>
      <el-input
        style="width:300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend">商品</template>
        <el-button slot="append" icon="el-icon-search" @click="commodityBtn"></el-button>
      </el-input>
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="search.spec"
        placeholder="请输入规格型号"
        clearable
      ></el-input>
      <el-select
        v-model="search.kind"
        multiple
        collapse-tags
        style="margin-left: 20px;"
        placeholder="请选择收发存类型"
      >
        <el-option
          v-for="item in businessType"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
      >查询</el-button>
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
      >清空</el-button>
      <el-button class="info" icon="el-icon-refresh" size="small" type="success" @click="refresh">刷新</el-button>
      <el-button type="info" size="small" icon="el-icon-tickets" @click="derive()">导出</el-button>
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      row-class-name="table-row-class"
      ref="table"
    >
      <el-table-column label="仓库/商品信息" align="center">
        <el-table-column prop="serial_code" label="单据编号" align="center"></el-table-column>
        <el-table-column prop="type" label="单据类型" align="center"></el-table-column>
        <el-table-column prop="date" label="日期" align="center"></el-table-column>
        <el-table-column prop="warehouse_code" label="仓库编码" align="center"></el-table-column>
        <el-table-column prop="warehouse" label="仓库名称" align="center"></el-table-column>
        <el-table-column prop="goods_code" label="商品编码" align="center"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="spec" label="规格型号" align="center"></el-table-column>
        <el-table-column prop="unit" label="单位" align="center"></el-table-column>
      </el-table-column>
      <el-table-column label="本期入库" align="center">
        <el-table-column prop="current_stage_inp_num" label="数量" align="center"></el-table-column>
        <el-table-column prop="current_stage_inp_avg_price" label="平均单价" align="center"></el-table-column>
        <el-table-column prop="current_stage_inp_amount" label="成本金额" align="center"></el-table-column>
      </el-table-column>
      <el-table-column label="本期出库" align="center">
        <el-table-column prop="current_stage_out_num" label="数量" align="center"></el-table-column>
        <el-table-column prop="current_stage_out_avg_price" label="平均单价" align="center"></el-table-column>
        <el-table-column prop="current_stage_out_amount" label="成本金额" align="center"></el-table-column>
      </el-table-column>
      <el-table-column label="期末结存" align="center">
        <el-table-column prop="end_stage_num" label="数量" align="center"></el-table-column>
        <el-table-column prop="end_stage_avg_price" label="平均单价" align="center"></el-table-column>
        <el-table-column prop="end_stage_amount" label="成本金额" align="center"></el-table-column>
      </el-table-column>
      <div style="display:block;background:red">加载</div>
    </el-table>
    <!-- 分页 -->
    <!-- <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination> -->
    <commodityDialog :commoditydialog="commoditydialog" @tag="commodityTag"></commodityDialog>
  </div>
</template>
<script>
import { getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import CommodityDialog from "@/components/grossMargin/commodityDialog";
import { wmsDetailReport } from "@/api/grossMargin/grossMarginList";
import { getToken } from "@/utils/auth";
import { classifyList } from "@/api/commodity/commodityList";

export default {
  components: {
    pagination: Pagination,
    commodityDialog: CommodityDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      container: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      businessType: [
        {
          value: "1_10",
          label: "购货入库"
        },
        {
          value: "1_20",
          label: "盘点入库"
        },
        {
          value: "1_30",
          label: "退货入库"
        },
        {
          value: "1_40",
          label: "分货入库"
        },
        {
          value: "1_50",
          label: "赠品入库"
        },
        {
          value: "1_60",
          label: "分拣入库"
        },
        {
          value: "1_70",
          label: "装箱入库"
        },
        {
          value: "0_10",
          label: "返货出库"
        },
        {
          value: "0_20",
          label: "盘点出库"
        },
        {
          value: "0_40",
          label: "分货出库"
        },
        {
          value: "0_60",
          label: "分拣出库"
        },
        {
          value: "0_70",
          label: "装箱出库"
        }
      ],
      //默认时间
      normal_startime: getnowDate().newStarTimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        goods_id: "",
        goods_code: "",
        start_time: "",
        end_time: "",
        kind: [],
        code: "",
        spec: ""
      },
      //dialog信息
      commoditydialog: {
        centerDialogVisible: false
      },
      commodityNameList: {
        name: "",
        code: "",
        id: ""
      },
      outer: {
        options: [],
        value: "",
        list: [],
        loading: false,
        id: ""
      },
      date: []
    };
  },
  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    // this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let Arrkind = this.search.kind;
      var kindvalue = Arrkind.join(",");

      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        goods_id: this.commodityNameList.id,
        goods_code: this.commodityNameList.code,
        start_date: this.search.start_time,
        end_date: this.search.end_time,
        kind: kindvalue,
        code: this.search.code,
        spec: this.search.spec
      };

      wmsDetailReport(data)
        .then(res => {
          if (res.code == 0) {
            this.container = res.data.summary;
            this.tableData = this.container.slice(0, this.pageData.page * 10);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: "",
        id: ""
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      let arrId = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
        arrId.push(res.id);
        console.log("+++++++" + arrId);
      });

      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
      this.commodityNameList.id = arrId.join(",");
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    // 本地搜索
    handleSearch() {
      if (this.date) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.tableData = [];
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    // 导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let Arrkind = this.search.kind;
          var kindvalue = Arrkind.join(",");
          let link = document.createElement("a");
           if (this.date) {
            this.search.start_time = this.date[0];
            this.search.end_time = this.date[1];
          }
          link.href =
            this.$axios.defaults.baseURL +
            "/api/warereport/exportWmsDetailReport?token=" +
            getToken() +
            "&goods_id=" +
            this.commodityNameList.id +
            "&goods_code=" +
            this.commodityNameList.code +
            "&start_date=" +
            this.search.start_time +
            "&end_date=" +
            this.search.end_time +
            "&kind=" +
            kindvalue +
            "&code=" +
            this.search.code +
            "&spec=" +
            this.search.spec;

          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //刷新
    refresh() {
      this.empty();
      this.getData();
    },
    //清空
    empty() {
      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        type: "",
        goods_id: "",
        goods_code: "",
        start_time: this.normal_startime,
        end_time: this.normal_endtime,
        kind: [],
        code: "",
        spec: ""
      };
      this.commodityNameList.name = "";
      this.commodityNameList.id = "";
      this.commodityNameList.code = "";
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    getMoreLog() {
      let arr = this.container.slice(
        this.pageData.page * 10,
        (this.pageData.page + 1) * 10
      );
      if (arr.length == 0) {
        this.$message("没有更多数据");
      } else {
        arr.forEach(res => {
          this.tableData.push(res);
        });
        this.pageData.page += 1;
      }
    }
  },
  mounted() {
    // 获取需要绑定的table
    this.dom = this.$refs.table.bodyWrapper;
    this.dom.addEventListener("scroll", () => {
      // 滚动距离
      let scrollTop = this.dom.scrollTop;
      // 变量windowHeight是可视区的高度
      let windowHeight = this.dom.clientHeight || this.dom.clientHeight;
      // 变量scrollHeight是滚动条的总高度
      let scrollHeight = this.dom.scrollHeight || this.dom.scrollHeight;
      if (scrollTop + windowHeight === scrollHeight) {
        // 获取到的不是全部数据 当滚动到底部 继续获取新的数据
        if (!this.allData) this.getMoreLog();
      }
    });
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>